package com.jqsoft.launcher.print;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintJob;
import android.print.PrintManager;
import android.support.v4.print.PrintHelper;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.jqsoft.launcher.common.Constant;
import com.jqsoft.launcher.util.StringTools;
import com.jqsoft.launcher.util.ToastUtil;

import java.util.UUID;

/**
 * Created by Administrator on 2017-10-11.
 */

@SuppressLint("NewApi")
public class PrintUtil {
    public static void printBitmap(final Context context, Bitmap bitmap){
        if (context==null){
            return;
        } else if (bitmap==null){
            ToastUtil.showToast(context, Constant.PRINT_HINT_BITMAP_NULL);
        } else {
            boolean supportPrint = checkWhetherSupportPrint(context);
            if (!supportPrint){
                return;
            }
            PrintHelper printHelper = new PrintHelper(context);
            printHelper.setColorMode(PrintHelper.COLOR_MODE_COLOR);
            printHelper.setOrientation(PrintHelper.ORIENTATION_LANDSCAPE);
            printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
            String imageName = getRandomUuid();
            printHelper.printBitmap(imageName, bitmap, new PrintHelper.OnPrintFinishCallback() {
                @Override
                public void onFinish() {
//                    ToastUtil.showToast(context, Constant.PRINT_HINT_PRINT_SUCCESS);
                }
            });
        }
    }

    public static void printHtmlStringByWebView(final Context context, String htmlDocument) {
        if (context==null){
            return;
        } else if (StringTools.isBlank(htmlDocument)){
            ToastUtil.showToast(context, Constant.PRINT_HINT_HTML_DOCUMENT_EMPTY);
        } else {
            boolean supportPrint = checkWhetherSupportPrint(context);
            if (!supportPrint){
                return;
            }
            WebView webView = new WebView(context);
            webView.setWebViewClient(new WebViewClient() {

                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return false;
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    createWebPrintJob(context, view);
                }
            });

            webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null);

        }
    }

    public static void printUrlByWebView(final Context context, String url) {
        if (context==null){
            return;
        } else if (StringTools.isBlank(url)){
            ToastUtil.showToast(context, Constant.PRINT_HINT_HTML_DOCUMENT_EMPTY);
        } else {
            boolean supportPrint = checkWhetherSupportPrint(context);
            if (!supportPrint){
                return;
            }
            WebView webView = new WebView(context);
            webView.setWebViewClient(new WebViewClient() {

                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return false;
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    createWebPrintJob(context, view);
                }
            });

            webView.loadUrl(url);
        }
    }

    public static void printCustomDocument(Context context){
        if (context==null){
            return;
        } else {
            boolean supportPrint = checkWhetherSupportPrint(context);
            if (!supportPrint){
                return;
            }

            PrintManager printManager = (PrintManager) context
                    .getSystemService(Context.PRINT_SERVICE);

            String jobName = getRandomUuid();

            printManager.print(jobName, new MyPrintDocumentAdapter(context),
                    null);

        }

    }

    public static void createWebPrintJob(Context context, WebView webView) {
        if (context==null){
            return;
        }
        PrintManager printManager = (PrintManager) context
                .getSystemService(Context.PRINT_SERVICE);

        PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

        String jobName = getRandomUuid();
        PrintJob printJob = printManager.print(jobName, printAdapter,
                new PrintAttributes.Builder().build());

//        // Save the job object for later status checking
//        mPrintJobs.add(printJob);
    }

    public static boolean checkWhetherSupportPrint(Context context){
        boolean supportPrint = PrintHelper.systemSupportsPrint();
        if (!supportPrint){
            ToastUtil.showToast(context, Constant.PRINT_HINT_NOT_SUPPORT_PRINT);
            return false;
        } else {
            return true;
        }
    }

    public static String getRandomUuid(){
        return UUID.randomUUID().toString();
    }
}
